﻿<?php
/**
 * Created by JetBrains PhpStorm.
 * User: ncduc
 * Date: 6/16/13
 * Time: 11:34 PM
 * To change this template use File | Settings | File Templates.
 */
require_once("setup.php");
function connect_database(Smarty &$smarty)
{
  
    
    $mysqli = mysqli_connect($smarty->getConfigVars('host'),$smarty->getConfigVars('user'),$smarty->getConfigVars('pass'),$smarty->getConfigVars('db'));
    $mysqli->query("set names 'utf8'");
    return $mysqli;
}
function get_lang()
{
    return $_COOKIE['lang_vinastemcelllab'];
}
function system_failed( $message = NULL, Smarty &$smarty)
{
    if(empty($message))
        $message ="Hệ thống đang gặp lỗi. Vui lòng liên hệ admin.";

    $smarty->assign('title','Lỗi hệ thống');
    $smarty->assign('message',$message);
    $smarty->assign('message_class','error');
    $smarty->display('notification.tpl');
}
function notify($title='',$message='',$notification_type, Smarty &$smarty)
{
    $smarty->assign('title',$title);
    $smarty->assign('message',$message);
    $smarty->assign('message_class',$notification_type);
    $smarty->display('notification.tpl');
}
function page_not_found()
{
    $message ="Đường dẫn bạn yêu cầu không tìm thấy trong hệ thống";
    $smarty = new SmartyML();
    $smarty->assign('title','Lỗi trang không tìm thấy');
    $smarty->assign('message',$message);
    $smarty->assign('message_class','error');
    $smarty->display('notification.tpl');
}
function check_user_session()
{
    if(isset ($_SESSION['email']))
    {
        if(time() - $_SESSION['last_activity'] > $_SESSION['expire_time'])
        {
            session_unset();
            session_regenerate_id(true);
            return false;
        }
        else
        {
            $_SESSION['last_activity'] = time();
            return true;
        }

    }
}
function check_admin_session()
{
    if(isset ($_SESSION['email'])&&isset($_SESSION['role']))
    {
        if($_SESSION['role']=='admin')
        {if(time() - $_SESSION['last_activity'] > $_SESSION['expire_time'])
        {
            session_unset();
            session_regenerate_id(true);
            return false;
        }
        else
        {
            $_SESSION['last_activity'] = time();
            return true;
        }}

    }
}
function send_mail($subject, $arr_email, $message)
{
    $to=""; 
    foreach($arr_email as &$email_name )
    {
        $email = $email_name[0];
        $name = $email_name[1];
        if($to!="")
            $to = $to . ",";
        $to = $to.$name."<".$email.">";
                
    }
    $header = 'From: webmaster@vinastemcelllab.com' . "\r\n" .
    'Reply-To: noreply@vinastemcelllab.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();
if(mail($to,$subject,$message,$header))
return true;
return false;
}
function send_mail2($subject, $arr_email, $message)
{
    require("libs/phpmailer/class.phpmailer.php");
    $mail = new PHPMailer();
    $mail->IsSMTP(); // send via SMTP
    $mail->SMTPAuth = true; // turn on SMTP authentication
    $mail->SMTPSecure = "tls";
    $mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
    $mail->Port = 587; // set the SMTP port for the GMAIL
    $mail->Username = "ptn.tebaogoc@gmail.com"; // SMTP username
    $mail->Password = "abcdabcd"; // SMTP password
    $webmaster_email = "ptn.tebaogoc@gmail.com"; //Reply to this email ID
    $mail->From = $webmaster_email;
    $mail->FromName = "Webmaster";

    foreach($arr_email as &$email_name )
    {
        $email = $email_name[0];
        $name = $email_name[1];
        $mail->AddAddress($email, $name);
    }
    $mail->AddReplyTo($webmaster_email, "Webmaster");
    $mail->WordWrap = 50; // set word wrap
    $mail->IsHTML(false); // send as HTML
    $mail->Subject = $subject;
    $mail->Body = $message;
    $mail->AltBody = $message; //Text Body
    if (!$mail->Send()) {
        $mail->ErrorInfo;
        return false;
    } else {
        return true;
    }
}
function assign_home_header(Smarty &$smarty)
{
    $smarty->assign('hoten_header',$_SESSION['hoten']);
}
function save_upload_file ($file,$table,$col,$id,  mysqli $mysqli, Smarty $smarty)
{
     $upload_result = upload_file($file, $id);
    if (strlen($upload_result) > 0) //upload file error
    {
        $message = $_FILES[$file]['name'].": ". $upload_result;
        $smarty->assign('is_display', "block");
        $smarty->assign('message', $message);
    } else {
        $filename = $_FILES[$file]['name'];
        $sql = "UPDATE $table SET $col = '{$filename}' WHERE Ma = {$id}";
        $mysqli->query($sql);
        if ($mysqli->error) {
            system_failed($mysqli->error . "sql: " . $sql, $smarty);
            $mysqli->close();
            return false;
        }
    }
    return true;
}
function upload_file ($file, $userid)
{
    $allowed_exts = array("gif", "jpeg", "jpg", "png","pdf","doc","docx");
    $temp = explode(".",$_FILES[$file]['name']);
    $extension = end($temp);
    if ((($_FILES[$file]["type"] == "image/gif")
            || ($_FILES[$file]["type"] == "image/jpeg")
            || ($_FILES[$file]["type"] == "image/jpg")
            || ($_FILES[$file]["type"] == "image/pjpeg")
            || ($_FILES[$file]["type"] == "image/x-png")
            || ($_FILES[$file]["type"] == "image/png")
            || ($_FILES[$file]["type"] == "application/pdf")
            ||($extension == "doc")
            ||($extension == "docx")
            )
        && ($_FILES[$file]["size"] < 10000000)//size must be less than 10 mb
        && in_array($extension, $allowed_exts))
    {
        if ($_FILES[$file]["error"] > 0)
        {
            return $_FILES[$file]["error"];
        }
        else
        {
            $upload_folder = $_SERVER['DOCUMENT_ROOT'] . "/oms/uploads/".$userid;
            if (file_exists($upload_folder. "/" . $_FILES[$file]["name"]))
            {
                return $_FILES[$file]["name"] . " already exists. ";
            }
            else
            {
                move_uploaded_file($_FILES[$file]["tmp_name"],
                    $upload_folder . "/" . $_FILES[$file]["name"]);
                return "";
            }

        }
    }
    else
    {
        return "Invalid file";
    }
}